// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_retrieve_respawngear.sqf"
#include "x_setup.sqf"

private ["_p","_bpadded"];
_p = player;

removeAllWeapons _p;
removeAllItems _p;
removeAllAssignedItems _p;
while {count magazines _p > 0} do {
	_p removeMagazines ((magazines _p) select 0);
};
{if !([_x] call FUNC(isBinocular)) then {_p linkItem _x} else {_p addWeapon _x}} forEach __pGetVar(GVAR(respawn_assignedItems));

_runi = __pGetVar(GVAR(respawn_uniform));
__TRACE_1("","_runi");
removeUniform _p;
_p addUniform _runi;

_rvest = __pGetVar(GVAR(respawn_vest));
__TRACE_1("","_rvest");
removeVest _p;
_p addVest _rvest;

_backp = __pGetVar(GVAR(respawn_backpack));
__TRACE_1("","_backp");
removeBackpack _p;
if (_backp != "") then {
	_p addBackpack _backp;
	clearBackpackCargoGlobal _p;
	clearAllItemsFromBackpack _p;
};

_backbackp = __pGetVar(GVAR(respawn_backpack_backpack));
__TRACE_1("","_backbackp");
if (count _backbackp > 0) then {
	_unitbp = unitBackpack _p;
	if (!isNull _unitbp) then {
		{
			__TRACE_1("adding backpack","_x");
			_unitbp addBackpackCargoGlobal [_x, 1];
		} forEach _backbackp;
	};
};

_bpadded = false;
if (isNull unitBackpack _p) then {
	__TRACE("add dummy backpack");
	_p addBackpack "B_HuntingBackpack";
	_bpadded = true;
};

_magsammofull =+ __pGetVar(GVAR(respawn_magazinesAmmoFull));

_xupd = false;
{
	if (_x select 2 && {!([(_x select 0)] call FUNC(isgrenade))}) then {
		__TRACE_1("loaded mag","_x");
		_p addMagazine [_x select 0, _x select 1];
		_magsammofull set [_forEachIndex, -1];
		_xupd = true;
	};
} forEach _magsammofull;
if (_xupd) then {
	_magsammofull = _magsammofull - [-1];
};

_rsecw = __pGetVar(GVAR(respawn_secondaryWeapon));
__TRACE_1("","_rsecw");
if (_rsecw != "") then {
	_p addWeapon _rsecw;
	{
		if (_x != "") then {
			__TRACE_1("secWeapItem","_x");
			_p addSecondaryWeaponItem _x;
		}
	} forEach __pGetVar(GVAR(respawn_secweapitems));
};

_rhandgw = __pGetVar(GVAR(respawn_handgunWeapon));
if (_rhandgw != "") then {
	_p addWeapon _rhandgw;
	removeAllHandgunItems _p;
	{
		if (_x != "") then {
			__TRACE_1("handGunItem","_x");
			_p addHandgunItem _x;
		}
	} forEach __pGetVar(GVAR(respawn_handgunItems));
};

_rprimw = __pGetVar(GVAR(respawn_primaryWeapon));
if (_rprimw != "") then {
	_p addWeapon _rprimw;
	removeAllPrimaryWeaponItems _p;
	{
		if (_x != "") then {
			__TRACE_1("primWeapItem","_x");
			_p addPrimaryWeaponItem _x;
		}
	} forEach __pGetVar(GVAR(respawn_primweapitems));
};

if (_bpadded) then {
	removeBackpack _p;
};

_gogs = __pGetVar(GVAR(respawn_goggles));
__TRACE_1("","_gogs");
if (goggles _p != _gogs) then {
	removeGoggles _p;
	_p addGoggles _gogs;
};

_headg = __pGetVar(GVAR(respawn_headgear));
__TRACE_1("","_headg");
if (headgear _p != _headg) then {
	removeHeadgear _p;
	_p addHeadgear _headg;
};

_grenademags = [];
{
	_ammo = getText(configFile/"CfgMagazines"/(_x select 0)/"ammo");
	if (getText(configFile/"CfgAmmo"/_ammo/"simulation") != "shotGrenade") then {
		_p addMagazine [_x select 0, _x select 1];
		__TRACE_2("","_x","_ammo");
	} else {
		_grenademags set [count _grenademags, [_x select 0, _x select 1, _x select 4]];
	};
} forEach _magsammofull;

_grenademags spawn {
	sleep 5;
	__TRACE_1("spawn _grenademags","_this");
	{
		__TRACE_1("spawn _grenademags","_x");
		player addMagazine [(_x select 0), 1];
	} forEach _this;
};

{
	__TRACE_1(" item","_x");
	_p addItem _x;
} forEach __pGetVar(GVAR(respawn_items));

_primw = primaryWeapon _p;
if (_primw != "") then {
	_p selectWeapon _primw;
	_muzzles = getArray(configFile/"cfgWeapons"/_primw/"muzzles");
	_p selectWeapon (_muzzles select 0);
};